<?php
/**
 * Cart page controller
 *
 * @author Dima
 *
 */

class CartController extends modules_default_controllers_ControllerBase
{
	protected $_bForLoggedUsersOnly = true;

//----------------------------------------------------------------------------------------------------

	public $ajaxable = array(
		'remove' => array('json'),
		'update' => array('json'),
	);

//----------------------------------------------------------------------------------------------------

	public function init()
	{
		if (! $this->getRequest()->isXmlHttpRequest())
		{
			$this->view->title = 'Cart';
			parent::init();

			$this->view->headScript()->appendFile('/js/cart.js');
		}

		$this->_helper->AjaxContext()->initContext('json');
	}

//----------------------------------------------------------------------------------------------------

	public function indexAction()
	{
		$orders = Lemar_Shop::getUserOrders();

		$items = array();

		$ordersFiltered = array();

		foreach ($orders as $order)
		{
			$orderItems = models_OrderMapper::getItems($order->id);
			if (count($orderItems) > 0)
			{
				$items[$order->id] 	= $orderItems;
				$ordersFiltered[] 	= $order;
			}
		}

		$this->view->orders = $ordersFiltered;
		$this->view->items 	= $items;
	}

//----------------------------------------------------------------------------------------------------

	public function addAction()
	{
		$oRequest 		= $this->getRequest();

		$orders 		= Lemar_Shop::getUserOrders();
		$idOrder		= null;
		if (isset($orders[0]->id))
			$idOrder	= $orders[0]->id;

		$idItem 		= (int)$oRequest->getParam('id');
		$size 			= (int)$oRequest->getParam('size');

		$params			= $oRequest->getParams();

		if ($this->activeMailingListId)
		{
			$addressItems = models_MailinglistMapper::findItemsByListId($this->activeMailingListId);
			$placeholders = array(
				'%title%',		'%first%',		'%last%',			'%company%',
				'%address%',	'%city%',		'%zip%',			'%country%',
				'%service%',	'%rank%',		'%unit%',			'%operation%',
				'%bfpo%',		'%birthday%',	'%anniversary%',	'%custom1%',
				'%custom2%',	'%custom3%',	'%custom4%',		'%custom5%'
			);
		}

		if ($this->activeMailingListId)
		{
			foreach ($addressItems as $item)
			{
				$idOrderItem	= Lemar_Shop::addToCart($idOrder, $idItem, $size, 1);

				models_OrderItemMapper::update($idOrderItem, array('address_id' => $item->id), 'models_DbTable_OrderItem');

				$replace = array(
					$item->title,		$item->firstName,		$item->lastName,
					$item->company,		$item->address,			$item->city,
					$item->zip,			$item->country,			$item->service,
					$item->rank,		$item->unit,			$item->operation,
					$item->bfpo,		$item->birthday,		$item->anniversary,
					$item->custom1,		$item->custom2,			$item->custom3,
					$item->custom4,		$item->custom5
				);

				$newParams = $params;

				foreach ($newParams['type'] as $key=>$type)
				{
					if ('text' == $type)
					{
						$newParams['text'][$key] = str_replace($placeholders, $replace, $newParams['text'][$key]);
					}
				}

				Lemar_Shop::saveResources($idOrderItem, $newParams);
			}

			$oZendSession = Zend_Registry::getInstance()->get('Zend_Session_Namespace');
			unset($oZendSession->mailingListId);
		}
		else
		{
			$idOrderItem	= Lemar_Shop::addToCart($idOrder, $idItem, $size, 1);
			Lemar_Shop::saveResources($idOrderItem, $params);
		}

		$this->_redirect('/cart');
	}
//----------------------------------------------------------------------------------------------------

	public function updateAction()
	{
		$oRequest 		= $this->getRequest();

		$id 			= (int)$oRequest->getParam('id', 0);
		$quantity 		= (int)$oRequest->getParam('quantity', 1);

		$this->view->valid 	= Lemar_Shop::updateCartItemQuantity($id, $quantity);
	}

//----------------------------------------------------------------------------------------------------

	public function removeAction()
	{
		$oRequest 		= $this->getRequest();

		$idItem 		= (int)$oRequest->getParam('id', null);

		$this->view->valid 	= Lemar_Shop::removeFromCart($idItem);
		$this->view->id 	= $idItem;
	}

//----------------------------------------------------------------------------------------------------

}
